/* Data2.do (STATA)
	Construct amount outstanding.
	by Ralph Koijen & Motohiro Yogo */

#delimit ;
clear all;
set more off;
set type double;

cap log close;
log using Data2, replace;


/* Step 1: Construct stock market capitalization */

/* Load data */

u year Counterpart outstand market
	using Data0, clear;

/* Extrapolate equity outstanding forward with MSCI market value */

sort Counterpart year;
by Counterpart: replace outstand = outstand[_n-1]*market/market[_n-1] if missing(outstand);

/* Extrapolate equity outstanding backward with MSCI market value */

gsort Counterpart -year;
by Counterpart: replace outstand = outstand[_n-1]*market/market[_n-1] if missing(outstand);

drop market;

/* Drop missing data */

drop if missing(outstand);

/* Construct variables */

gen byte type = 3;
gen currency = "ALL";

/* Save data */

tempfile stock;
save `stock';


/* Step 2: Construct amount outstanding with OECD data */

/* Load OECD data */

u OECD, clear;

/* Merge MSCI market value */

merge m:1 year Counterpart using Data0,
	keepusing(market);

replace type = 3 if _merge==2;

drop _merge;

/* Extrapolate equity outstanding forward with MSCI market value */

sort Counterpart type year;
by Counterpart type: replace outstand = outstand[_n-1]*market/market[_n-1] if missing(outstand);

/* Extrapolate equity outstanding backward with MSCI market value */

gsort Counterpart type -year;
by Counterpart type: replace outstand = outstand[_n-1]*market/market[_n-1] if missing(outstand);

drop market;

/* Drop missing data */

drop if missing(outstand);

/* Construct variables */

gen currency = "ALL";


/* Step 3: Merge BIS data and stock market capitalization */

/* Merge BIS data */

merge m:1 year Counterpart type currency using BIS;

gen byte source = 1 if _merge!=2;
replace source = 2 if _merge==2;

drop _merge;

/* Substract international debt securities from OECD data */

replace outstand = max(outstand-ids,0) if !missing(outstand-ids);

/* Extrapolate OECD data backward with BIS data */

gsort Counterpart type currency -year;
by Counterpart type currency: replace outstand = outstand[_n-1]*dds/dds[_n-1] if missing(outstand);

/* Construct shares in ST and LT debt within domestic debt securities */

egen Tdds = total(dds) if inlist(type,1,2), missing by(year Counterpart currency);

gen share = dds/Tdds if inlist(type,1,2);

drop Tdds;

/* Extrapolate backward */

gsort Counterpart type currency -year;
by Counterpart type currency: replace share = share[_n-1] if missing(share);

/* Impute ST and LT debt from All debt */

sort year Counterpart currency type;
by year Counterpart currency: replace outstand = outstand[1]*share if type[1]==0 & missing(outstand);

drop share;

/* Use BIS data if OECD not available */

replace outstand = dds if missing(outstand);

drop dds;

/* Construct shares in ST and LT debt within international debt securities */

egen Tids = total(ids) if inlist(type,1,2), missing by(year Counterpart currency);

gen share = ids/Tids if inlist(type,1,2);

drop Tids;

/* Impute ST and LT debt from All debt */

sort year Counterpart currency type;
by year Counterpart currency: replace outstand = outstand[1]*share if type[1]==0 & missing(outstand);

drop share;

/* Drop All Debt */

drop if type==0;

/* Include international debt securities */

replace outstand = ids if currency!="ALL";

drop ids;

/* Merge stock market capitalization */

merge m:1 year Counterpart type using `stock';

replace source = 3 if _merge==2;

drop _merge;


/* Step 4: Restate and aggregate by nationality */

/* Summarize */

table Counterpart type year if currency=="ALL", nototals stat(max source);

/* Merge restatement matrices for issuances */

joinby year Counterpart type using Restatement_issuance, unmatched(master);

drop _merge;

/* Assume nationality is same as residency for fund shares */

replace counterpart = Counterpart if type==4;
replace Value = 1 if type==4;

/* Merge country code */

merge m:1 counterpart using Countries,
	keepusing(Yeuro Ynat)
	nogen keep(master match);

/* Restate from residency to nationality */

replace outstand = outstand*Value;

drop Value;

/* Construct dummy for domestic currency */

gen byte Idomestic = currency==counterpart | (currency=="EUR" & year>=Yeuro) | currency=="ALL";

drop currency Yeuro;

/* Keep sample of countries */

keep if year>=Ynat;

drop Ynat;

/* Aggregate by nationality */

collapse (sum) outstand, by(year counterpart type Idomestic) fast;

/* Label variables */

order year counterpart type Idomestic;

label var Idomestic	"Dummy: Domestic currency";

label var outstand 	"Amount outstanding (US$ billion)";

/* Save data */

sort year counterpart type Idomestic;

save Data2, replace;

log close;
